package com.arvin.faceDetect.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.arvin.faceDetect.R
import com.arvin.faceDetect.camera.CameraManager
import com.arvin.faceDetect.databinding.FragmentCameraSettingsBinding
import com.arvin.faceDetect.model.CameraSettings
import com.arvin.faceDetect.utils.Settings

class CameraSettingsFragment : Fragment() {
    private var _binding: FragmentCameraSettingsBinding? = null
    private val binding get() = _binding!!
    private val cameraManager = CameraManager.getInstance()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentCameraSettingsBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 设置返回按钮
        binding.toolbar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }

        // 加载当前设置
        loadCurrentSettings()

        // 设置保存按钮点击事件
        binding.btnSave.setOnClickListener {
            saveSettings()
        }

        // 设置开关状态改变监听器
        binding.switchShowFaceRect.setOnCheckedChangeListener { _, isChecked ->
            Settings.setShowFaceRect(requireContext(), isChecked)
        }
        
        binding.switchShowEyeRect.setOnCheckedChangeListener { _, isChecked ->
            Settings.setShowEyeRect(requireContext(), isChecked)
        }

        binding.switchAutoFocus.setOnCheckedChangeListener { _, isChecked ->
            Settings.setAutoFocusEnabled(requireContext(), isChecked)
        }

        binding.switchFlash.setOnCheckedChangeListener { _, isChecked ->
            Settings.setFlashEnabled(requireContext(), isChecked)
        }

        // 设置摄像头切换监听器
        binding.rgCameraFacing.setOnCheckedChangeListener { _, checkedId ->
            val cameraFacing = when (checkedId) {
                R.id.rbCameraFront -> CameraSettings.CAMERA_FACING_FRONT
                R.id.rbCameraBack -> CameraSettings.CAMERA_FACING_BACK
                else -> CameraSettings.CAMERA_FACING_FRONT
            }
            Settings.setCameraFacing(requireContext(), cameraFacing)
        }
    }

    private fun loadCurrentSettings() {
        val context = requireContext()
        
        // 加载分辨率设置
        when (Settings.getCameraResolution(context)) {
            Settings.CameraResolution.HIGH -> binding.rbResolutionHigh.isChecked = true
            Settings.CameraResolution.MEDIUM -> binding.rbResolutionMedium.isChecked = true
            Settings.CameraResolution.LOW -> binding.rbResolutionLow.isChecked = true
        }

        // 加载摄像头方向设置
        when (Settings.getCameraFacing(context)) {
            CameraSettings.CAMERA_FACING_FRONT -> binding.rbCameraFront.isChecked = true
            CameraSettings.CAMERA_FACING_BACK -> binding.rbCameraBack.isChecked = true
            else -> binding.rbCameraFront.isChecked = true
        }

        // 加载其他设置
        binding.switchShowFaceRect.isChecked = Settings.isShowFaceRect(context)
        binding.switchShowEyeRect.isChecked = Settings.isShowEyeRect(context)
        binding.switchAutoFocus.isChecked = Settings.isAutoFocusEnabled(context)
        binding.switchFlash.isChecked = Settings.isFlashEnabled(context)
    }

    private fun saveSettings() {
        val context = requireContext()
        
        // 保存分辨率设置
        val resolution = when {
            binding.rbResolutionHigh.isChecked -> Settings.CameraResolution.HIGH
            binding.rbResolutionMedium.isChecked -> Settings.CameraResolution.MEDIUM
            else -> Settings.CameraResolution.LOW
        }
        Settings.setCameraResolution(context, resolution)

        // 保存摄像头方向设置
        val cameraFacing = when {
            binding.rbCameraFront.isChecked -> CameraSettings.CAMERA_FACING_FRONT
            binding.rbCameraBack.isChecked -> CameraSettings.CAMERA_FACING_BACK
            else -> CameraSettings.CAMERA_FACING_FRONT
        }
        Settings.setCameraFacing(context, cameraFacing)

        // 保存其他设置
        Settings.setShowFaceRect(context, binding.switchShowFaceRect.isChecked)
        Settings.setShowEyeRect(context, binding.switchShowEyeRect.isChecked)
        Settings.setAutoFocusEnabled(context, binding.switchAutoFocus.isChecked)
        Settings.setFlashEnabled(context, binding.switchFlash.isChecked)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
} 